﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }
    protected void BtnCalc_Click(object sender, EventArgs e)
    {
        double number1 = 0.0;
        double number2 = 0.0;
        double result = 0.0;
        bool valid_values = true;
        string error_message = "";
        if (!Double.TryParse(TextBox1.Text, out number1) || String.IsNullOrEmpty(TextBox1.Text)){
           valid_values =false;
           error_message = "Something is wrong with the inputs!";
            
        }
        else if (!Double.TryParse(TextBox2.Text, out number1) || String.IsNullOrEmpty(TextBox2.Text))
        {
            valid_values = false;
            error_message = "Something is wrong with the inputs!";
        }
        else
        {
            number1 = Convert.ToDouble(TextBox1.Text);
            number2 = Convert.ToDouble(TextBox2.Text);
        }
       
        string action = "";
        if (!(RadioButtonList1.SelectedIndex < 0) && (valid_values =true))
        {
            if (RadioButtonList1.SelectedItem.Value.Equals("sum", StringComparison.Ordinal))
            {
                result = number1 + number2;
                action = "+"; 
            }
            else if (RadioButtonList1.SelectedItem.Value.Equals("multiply", StringComparison.Ordinal))
            {
                result = number1 * number2;
                action = "*"; 
            }
            else if (RadioButtonList1.SelectedItem.Value.Equals("subtract", StringComparison.Ordinal))
            {
                result = number1 - number2;
                action = "-"; 
            }
            else if (RadioButtonList1.SelectedItem.Value.Equals("divide", StringComparison.Ordinal))
            {
                if (number2 == 0)
                {
                    error_message = "You Can't Divide by 0!";
                     
                }
                else { result = number1 / number2;
                action = "/";
                }
            }
        }
        else
        {
            error_message = "Select the mathematical operation!";
        }

        if (error_message.Length > 0)
        {
            TextBox3.Text = error_message;
            error_message = "";
        }
        else {
            TextBox3.Text = result.ToString();
            if (CheckBox1.Checked)
            {
                string list_box_string = String.Format("{0} {1} {2} = {3}",
                                  number1, action, number2, result);
                ListBox1.Items.Add(list_box_string);
            }
            ListBox1.SelectedIndex = ListBox1.Items.Count - 1;

        }

        


        

        
        
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        MyClearingMethod(form1);

    }
    private void MyClearingMethod(Control myForm)
    {
        foreach (Control item in myForm.Controls)
        {
            if (item is TextBox)
            {
                ((TextBox)item).Text = string.Empty;
            }
            else if (item is ListBox)
            {
                ((ListBox)item).Items.Clear();
            }
            else if (item is RadioButtonList) {
                (item as RadioButtonList).ClearSelection();
            }
            else if (item is CheckBox)
            {
                ((CheckBox)item).Checked = false;

            }
        }
    }
}